package com.curso.android.trabajo.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * @author David
 * 
 */
public class MisSitiosSQLHelper extends SQLiteOpenHelper {

	private static final String sqlCreate = "CREATE TABLE " + MisSitiosDB.Sitios.TABLE_NAME + " ("
			+ MisSitiosDB.Sitios._ID
			+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
			+ MisSitiosDB.Sitios.NAME + " TEXT,"
			+ MisSitiosDB.Sitios.DESCRIPTION + " TEXT,"
			+ MisSitiosDB.Sitios.LATITUDE + " FLOAT,"
			+ MisSitiosDB.Sitios.LONGITUDE + " FLOAT,"
			+ MisSitiosDB.Sitios.PHOTO + " TEXT " + ")";

	public MisSitiosSQLHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
	 * .SQLiteDatabase)
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {

		if (db.isReadOnly()) {
			db = getWritableDatabase();
		}

		db.execSQL(sqlCreate);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
	 * .SQLiteDatabase, int, int)
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// borrar la anterior
		db.execSQL("DROP TABLE IF EXISTS "+MisSitiosDB.Sitios.TABLE_NAME);

		// crear de nuevo
		db.execSQL(sqlCreate);

	}

}
